package online.exam.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import online.exam.common.StudentStateEnum;
import online.exam.mapper.StudentMapper;
import online.exam.pojo.Student;
import online.exam.pojo.bo.StudentBO;
import online.exam.service.StudentService;
import online.exam.utils.MD5Tools;
import online.exam.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author 王景煜
 */

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Student queryByNoAndPwd(StudentBO studentBO) {
        Example stuExample = new Example(Student.class);
        Example.Criteria stuCriteria = stuExample.createCriteria();
        stuCriteria.andEqualTo("studentNo", studentBO.getStuNo());
        stuCriteria.andEqualTo("password", MD5Tools.md5EncodingUtf8(studentBO.getStuPwd()));
        Student student = studentMapper.selectOneByExample(stuExample);
        if (student != null) {
            student.setPassword(null);
            return student;
        }
        return null;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PageUtils<Student> queryAll(Integer page, Integer limit) {
        Example studentExample = new Example(Student.class);
        Example.Criteria stuCriteria = studentExample.createCriteria();
        stuCriteria.andEqualTo("state", StudentStateEnum.ONLINE.type);
        PageHelper.startPage(page, limit);
        List<Student> studentList = studentMapper.selectByExample(studentExample);
        PageInfo<?> info = new PageInfo<>(studentList);
        PageUtils<Student> pageUtils = new PageUtils<>();
        pageUtils.setPage(page);
        pageUtils.setLimit(limit);
        pageUtils.setCount(info.getTotal());
        pageUtils.setData(studentList);
        return pageUtils;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Student queryByNo(String studentNo) {
        Example ste = new Example(Student.class);
        Example.Criteria stc = ste.createCriteria();
        stc.andEqualTo("studentNo", studentNo);
        return studentMapper.selectOneByExample(ste);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int addStudent(Student student) {
        student.setId(IdUtil.simpleUUID());
        student.setPassword(MD5Tools.md5EncodingUtf8("123456"));
        student.setLogo("http://122.152.205.72:88/group1/M00/00/05/CpoxxFw_8_qAIlFXAAAcIhVPdSg994.png");
        student.setState(StudentStateEnum.ONLINE.type);
        student.setCreatedTime(new Date());
        return studentMapper.insert(student) == 1 ? HttpStatus.HTTP_OK : HttpStatus.HTTP_INTERNAL_ERROR;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int updateStudent(Student student) {
        if (!StringUtils.isEmpty(student.getPassword()))
            student.setPassword(MD5Tools.md5EncodingUtf8(student.getPassword()));
        int result = studentMapper.updateByPrimaryKeySelective(student);
        return result == 1 ? HttpStatus.HTTP_OK : HttpStatus.HTTP_INTERNAL_ERROR;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int deleteStudent(String id) {
        if (id.contains(",")) {
            String[] ids = id.split("\",\"");
            List<String> idList = new ArrayList<>(Arrays.asList(ids));
            int result = studentMapper.deleteMorePojo(idList);
            return ids.length == result ? HttpStatus.HTTP_OK : HttpStatus.HTTP_INTERNAL_ERROR;
        }
        int result = studentMapper.deletePojo(id);
        return result > 0 ? HttpStatus.HTTP_OK : HttpStatus.HTTP_INTERNAL_ERROR;
    }

}
